Ext.namespace('Ext.csEmpDimission');
Ext.csEmpDimission.status = [['0', '新增'], ['1', '离职办理中'], ['2', '手续完成']];

var empDimissionDetailWin;

Ext.onReady(function() {

	empDimissionDetailWin = empDimissionDetailWin();

	var empDimissionPanel = new Ext.form.FormPanel({

		frame : true,
		hideLables : false,
		id : 'empDimissionPanel',
		buttonAlign : 'right',
		bodyStyle : 'padding:5px 5px 0',
		items : [{
					layout : 'column',
					items : [{
								columnWidth : .5,
								layout : 'form',
								items : [{
											xtype : 'textfield',
											fieldLabel : '雇员姓名',
											name : 'name_dim',
											id : 'name_dim',
											anchor : '90%'
										}]
							}, {
								columnWidth : .5,
								layout : 'form',
								items : [{
											xtype : 'textfield',
											fieldLabel : '雇员编号',
											name : 'empCsCode_dim',
											id : 'empCsCode_dim',
											anchor : '90%'
										}]

							}]

				}],
		buttons : [{
			text : '查询',
			handler : function() {
				empDimssionStore.on('beforeload', function() {
							empDimssionStore.baseParams = {
								'filter.empName' : Ext.getCmp('name_dim')
										.getValue(),
								'filter.empCsCode' : Ext
										.getCmp('empCsCode_dim').getValue()
							}
						});
				empDimssionStore.reload();
			}
		}, {
			text : '新增',
			handler : function() {
				Ext.getCmp('empDimissionDetialPanel').getForm().reset();
				Ext.getCmp('status').setValue(0);
				empDimissionDetailWin.findById('disId').setValue('');
				empDimissionDetailWin.findById('disMemo').setValue('');
				empDimissionDetailWin.findById('empCsCode').setValue('');
				empDimissionDetailWin.findById('employeeId').setValue('');
				empDimissionDetailWin.findById('name').setValue('');
				empDimissionDetailWin.findById('outJobdate').setValue('');
				empDimissionDetailWin.findById('payEnd').setValue('');
				empDimissionDetailWin.findById('socialEnd').setValue('');
				empDimissionDetailWin.findById('isValid').setValue('');
				empDimissionDetailWin.findById('isInjob').setValue('');
				Ext.getCmp('empCsCode').setDisabled(false);
				Ext.getCmp('name').setDisabled(false);
				Ext.getCmp('status').setDisabled(false);
				Ext.getCmp('_save').setDisabled(false);
				Ext.getCmp('_delete').setDisabled(true);
				Ext.getCmp('_doing').setDisabled(true);
				Ext.getCmp('_back').setDisabled(true);
				Ext.getCmp('_finish').setDisabled(true);
				empDimissionDetailWin.show();

			}
		}, {
			text : '删除',
			handler : function() {
				var deleteJson = [];
				var length = empDimssionGrid.getSelectionModel()
						.getSelections().length;
				if (length > 0) {
					Ext.MessageBox.show({
						title : '提示',
						msg : '您选中了' + length + '条记录进行删除,请问您确认吗?',
						buttons : Ext.MessageBox.OKCANCEL,
						fn : function(btn) {
							empDimssionGrid.stopEditing();
							if (btn == "ok") {
								var rs = empDimssionGrid.getSelectionModel()
										.getSelections();
								for (var i = 0; i < length; i++) {
									if (rs[i].data.status != 0) {
										Ext.Msg
												.alert('提示',
														'只有状态为"新增"的用户能被删除.');
										return;

									} else {
										Ext.Ajax.request({
											url : "csEmpDimissionAction!deleteDimission.action?empDimssionIds="
													+ empDimssionGrid.selModel.selections.keys,
											method : "post",
											success : function(response,
													options) {
												var responseTextVale = Ext.util.JSON
														.decode(response.responseText);
												if (responseTextVale.hasSubCity > 0) {
													Ext.Msg.alert('删除失败',
															'失败原因！');
												} else {
													empDimssionStore.reload();
													Ext.Msg.alert('提示',
															'删除操作成功！');
												}

											}
										});

									}

									empDimssionStore.remove(empDimssionStore
											.getById(rs[i].disId));
								};
							}
							if (btn == "cancel") {
								return;
							}
						}
					});
				} else {
					Ext.Msg.alert('警告', '请选择需要删除的记录.');
				}
			}
		}]

	});

	empDimssionStore = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : 'csEmpDimissionAction.action?actionType=0'
						}),
				reader : new Ext.data.JsonReader({
							root : 'result.result',
							id : 'disId',
							totalProperty : 'result.totalCount'
						}, [{
									name : 'disId',
									mapping : "disId"
								}, {
									name : 'empCsCode',
									mapping : "empCsCode"
								}, {
									name : 'employeeId',
									mapping : "employeeId"
								}, {
									name : 'name',
									mapping : "name"
								}, {
									name : 'orgchartName',
									mapping : 'orgchartName'
								}, {
									name : 'status',
									mapping : 'status'
								}])
			});

	// 查询显示的表格
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm, {
		header : '雇员编号',
		dataIndex : 'empCsCode',
		width : 150,
		renderer : function(value, p, record) {
			return "<a href='#' onclick=\"modifyInfoForDimission('"
					+ record.data.disId + "');return false;\">" + value
					+ "</a>";

		}
	}, {
		header : '雇员姓名',
		dataIndex : 'name',
		width : 150
	}, {
		header : '所属组织',
		dataIndex : 'orgchartName',
		width : 150
	}, {
		header : '状态',
		dataIndex : 'status',
		width : 150,
		renderer : function(value, p, record) {
			if (value == 0) {
				return '新增';
			} else if (value == 1) {
				return '离职办理中';
			} else if (value == 2) {
				return '手续完成';
			}
		}
	}]);

	empDimissionPanel.render(Ext.getBody());
	var empDimssionGrid = new Ext.grid.GridPanel({
				height : document.body.clientHeight
						- Ext.getCmp('empDimissionPanel').getInnerHeight()
						- Ext.getCmp('empDimissionPanel').getFrameHeight(),

				id : 'empDimssionGrid',
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,
							store : empDimssionStore,
							displayInfo : true,
							dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
							emptyMsg : '没有记录'
						}),
				cm : cm,
				sm : sm,
				store : empDimssionStore,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				}
			});
	empDimssionGrid.render(Ext.getBody());
	empDimssionStore.reload();
});

function modifyInfoForDimission(id) {
	updflag = true;
	Ext.Ajax.request({
		url : "csEmpDimissionAction!queryByID.action",
		params : {
			"id" : id
		},
		method : "post",
		success : function(response, options) {
			var responseTextValue = Ext.util.JSON.decode(response.responseText);
			// Ext.getCmp('empDimissionDetialPanel').getForm().setValues(responseTextValue.csEmpDimission);
			empDimissionDetailWin.findById('disId')
					.setValue(responseTextValue.csEmpDimission.disId);
			empDimissionDetailWin.findById('disMemo')
					.setValue(responseTextValue.csEmpDimission.disMemo);
			empDimissionDetailWin.findById('empCsCode')
					.setValue(responseTextValue.csEmpDimission.empCsCode);
			empDimissionDetailWin.findById('employeeId')
					.setValue(responseTextValue.csEmpDimission.employeeId);
			empDimissionDetailWin.findById('name')
					.setValue(responseTextValue.csEmpDimission.name);
			empDimissionDetailWin.findById('outJobdate')
					.setValue(responseTextValue.csEmpDimission.outJobdate
							.substr(0, 10));
			empDimissionDetailWin.findById('payEnd')
					.setValue(responseTextValue.csEmpDimission.payEnd);
			empDimissionDetailWin.findById('socialEnd')
					.setValue(responseTextValue.csEmpDimission.socialEnd);
			empDimissionDetailWin.findById('status')
					.setValue(responseTextValue.csEmpDimission.status);
			empDimissionDetailWin.findById('isValid')
					.setValue(responseTextValue.csEmpDimission.isValid);
			empDimissionDetailWin.findById('isInjob')
					.setValue(responseTextValue.csEmpDimission.isInjob);
			Ext.getCmp('empCsCode').setDisabled(true);
			Ext.getCmp('name').setDisabled(true);
			Ext.getCmp('status').setDisabled(true);
			if (responseTextValue.csEmpDimission.status == 1) {
				Ext.getCmp('_save').setDisabled(true);
				Ext.getCmp('_delete').setDisabled(true);
				Ext.getCmp('_doing').setDisabled(true);
				Ext.getCmp('_back').setDisabled(false);
				Ext.getCmp('_finish').setDisabled(false);
			}else if (responseTextValue.csEmpDimission.status == 2) {
				Ext.getCmp('_save').setDisabled(true);
				Ext.getCmp('_delete').setDisabled(true);
				Ext.getCmp('_doing').setDisabled(true);
				Ext.getCmp('_back').setDisabled(true);
				Ext.getCmp('_finish').setDisabled(true);
			} else {
				Ext.getCmp('_save').setDisabled(false);
				Ext.getCmp('_delete').setDisabled(false);
				Ext.getCmp('_doing').setDisabled(false);
				Ext.getCmp('_back').setDisabled(true);
				Ext.getCmp('_finish').setDisabled(true);
			}
			empDimissionDetailWin.show();
		}
	});
}